package jeconbond.economic.systems.market;

import java.util.List;

import laboratoryQ.computation.IRandom;
import laboratoryQ.BaseUtils;

public class SelectPropsStrat implements ISelectPropsStrategy {
	public static final SelectPropsStrat SELECT_ALL_INSTANCE = new SelectPropsStrat();
	private double percentPart = -1;
	private int constPart = -1;
	private IRandom random;
	
	public SelectPropsStrat() {
	}
	
	public SelectPropsStrat(double percentPart, IRandom random) {
		this.percentPart = percentPart;
		this.random = random;
	}
	
	public SelectPropsStrat(double percentPart, int constPart, IRandom random) {
		this.percentPart = percentPart;
		this.constPart = constPart; 
		this.random = random;
	}

	@Override
	public ISellProposition[] select(List<ISellProposition> props) {
		if (percentPart >= 0) {
			int size = (int) (props.size() * percentPart);
			if (props.size() > 0 && size == 0) {
				size = 1;
			}
			ISellProposition[] result = new ISellProposition[size];
			randomList2arr(props, result, random);
			return result;
		} else 
		if (constPart >= 0) {
			ISellProposition[] result = new ISellProposition[constPart];
			randomList2arr(props, result, random);
			return result;
		} else {
			ISellProposition[] result = new ISellProposition[props.size()];
			props.toArray(result);
			return result;
		}
	}

	private void randomList2arr(List<ISellProposition> props, ISellProposition[] dest, IRandom random) {
		for (int i = 0; i < dest.length; i++) {
			int pos = BaseUtils.randomInt(random, props.size());
			dest[i] = props.get(pos);
		}
	}
}
